package com.effectivcrm.startup;


import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.io.Resource;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.stereotype.Component;

import com.effectivcrm.domain.ui.Form;
import com.hazelcast.core.IMap;
import com.thoughtworks.xstream.XStream;

@Component
public class FormManager {
	
	@Value("${effectiv.form.dir}")
	private String location;
	
	@Value("${effectiv.form.filename.pattern}")
	private String filePattern;
	
	
	
	public void load(IMap<String,Form> formCache){
		PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
		
		String resourcePattern = location + filePattern;
		
		System.out.println("resourcePattern = " + resourcePattern);
		
		Resource resources[];
		try {
			resources = resolver.getResources(resourcePattern);
			
			for(Resource r : resources){
				System.out.println("Found - " + r.getFilename());
				
				XStream xStream = new XStream();
				xStream.processAnnotations(Form.class);
				Form form = (Form) xStream.fromXML(r.getInputStream());
				formCache.put(form.getPage(), form);
				
			}
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
		
		
	}
	
}
